<?php
/**
 * @user: thanatos <thanatos915@163.com>
 */

namespace frontend\controllers;


use common\models\Awards;
use common\models\Record;
use frontend\models\AwardsForm;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;

class PrizeController extends BaseController
{
    public $layout = 'prize';

    public function actionIndex()
    {
        // 查询奖品
        $list = Awards::find()->all();
        return $this->render('index', ['list' => $list]);
    }

    public function actionForm()
    {
        if (\Yii::$app->request->isPost) {
            $params = \Yii::$app->request->post();
            $model = new AwardsForm();
            if (!$result = $model->submit($params)) {
                var_dump($result);exit;
                return Json::encode(['status' => 'n', 'msg' => $model->getFirstError('')]);
            } else {
                return Json::encode(['status' => 'y', 'data' => $result]);
            }
        }
        return $this->render('form');
    }

    /**
     * @param $id
     * @throws ForbiddenHttpException
     * @author thanatos <thanatos915@163.com>
     */
    public function actionScraping($id)
    {
        $model = Record::findOne(['id' => $id]);
        if (empty($model)) {
            throw new ForbiddenHttpException();
        }
        return $this->render('scraping', ['model' => $model]);
    }

}